package com.media.utils;

import com.alibaba.fastjson.JSON;
import com.media.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @Author: 小何
 * @Description: TODO
 * @DateTime: 2023/10/30 15:33
 **/
@Slf4j
public class SecurityUtils {

    public static User getUser() {
        try {
            //拿 到当前用户身份
            Object principalObj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            if (principalObj instanceof String) {
                //取出用户身份信息
                String principal = principalObj.toString();
                //将json转成对象
                User user = JSON.parseObject(principal, User.class);
                return user;
            }
        } catch (Exception e) {
            log.error("获取当前登录用户身份出错:{}", e.getMessage());
            e.printStackTrace();
        }

        return null;
    }





}
